#!/bin/bash
echo "delete all apk file that path of app/build/outputs/apk/"
rm -rvf ./*/build/
arg=$1
if [ "${arg}" = "1" ]
    then echo "build Debug"
        ./gradlew assembleDebug
	elif [ "${arg}" = "2" ]
    then echo "build Release"
        ./gradlew assembleRelease
    elif [ -z "${arg}" ]
	then echo "build Debug"
	    ./gradlew assembleDebug
	else
	    echo "build ${arg}"
	    ./gradlew "${arg}"
fi

find ./app/build/outputs/apk/*/*.apk | while read name
do
    mapping=${name##*/}
    mapping=${mapping%.*}
    cp ./app/build/outputs/apk/*/*.apk ~/Documents/APK/vnc/
    if [ "${arg}" = "1" ]
        then
            ./sendmail.sh ${name} ${mapping}
        elif [ -z "${arg}" ]
        then
            echo "build end"
        else
            echo "copy mapping"
            cp -r ./app/build/outputs/mapping/  ~/Documents/APK/vnc/${mapping}
            ./sendmail.sh ${name} ${mapping}
    fi
done
open ~/Documents/APK/vnc/